package com.tcm.ucenter.web.controller;

import com.base.core.exception.ServiceException;
import com.base.core.pager.PageBean;
import com.base.core.utils.R;
import com.tcm.ucenter.domain.dto.UserAddressDTO;
import com.tcm.ucenter.service.UserAddressService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/ucenter/address")
public class UserAddressController {

    @DubboReference(version = "0.0.1-alpha", timeout = 3000, check = false, retries = 3)
    private UserAddressService userAddressService;

    /**
     * 根据id获取但条数据
     * @param id
     * @return
     * @throws Exception
     */
    @GetMapping("/view")
    public R view(@RequestParam("id") Long id) throws Exception {
        return R.success(userAddressService.findById(id));
    }

    /**
     * 新增收货地址
     * @param userAddressDto
     * @return
     */
    @PostMapping("/save")
    public R create(@RequestBody UserAddressDTO userAddressDto){
        try{
            userAddressService.save(userAddressDto);
        }catch (ServiceException e) {
            return R.fail(e.getResult().getCode(), e.getMessage());
        } catch (Exception e){
            return R.fail(e.getMessage());
        }
        return R.success();
    }

    /**
     * 更新数据
     * @param userAddress
     * @return
     */
    @PostMapping("/update")
    public R update(@RequestBody UserAddressDTO userAddress){
        try{
            userAddressService.update(userAddress);
        }catch (ServiceException e) {
            return R.fail(e.getResult().getCode(), e.getMessage());
        } catch (Exception e){
            return R.fail(e.getMessage());
        }
        return R.success();
    }

    /**
     * 逻辑删除
     * @param id
     * @return
     */
    @GetMapping("/delete")
    public R delete(@RequestParam("id") Long id){
        try{
            userAddressService.deleteLogic(id);
        }catch (ServiceException e) {
            return R.fail(e.getResult().getCode(), e.getMessage());
        } catch (Exception e){
            return R.fail(e.getMessage());
        }
        return R.success();
    }

    /**
     * 获取用户默认地址
     * @return
     */
    @GetMapping("/default")
    public R findDefaultAddress(@RequestParam("userId") Long userId){
        try {
            return R.success(userAddressService.findDefaultAddressByUserId(userId));
        } catch (ServiceException e) {
            e.printStackTrace();
            return R.fail(e.getResult().getCode(),e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
    }

    /**
     * 分页
     * @param userId
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping("/list/page")
    public R listpage(@RequestParam Long userId, @RequestParam(defaultValue = "1") Long page, @RequestParam(defaultValue = "10") Long pagesize){
        PageBean listBean;
        try {
            listBean = userAddressService.listPageByUserId(userId, page, pagesize);
        } catch (ServiceException e) {
            return R.fail(e.getResult().getCode(), e.getMessage());
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
        return R.success(listBean);
    }

}